package cn.ynrcb.personnelfile.admin.controller;

import cn.ynrcb.personnelfile.admin.entity.NoticeEntity;
import cn.ynrcb.personnelfile.admin.entity.NoticeSendEntity;
import cn.ynrcb.personnelfile.admin.service.NoticeSendService;
import cn.ynrcb.personnelfile.admin.service.NoticeService;
import cn.ynrcb.personnelfile.admin.vo.NoticeListVo;
import cn.ynrcb.personnelfile.admin.vo.NoticeVo;
import cn.ynrcb.personnelfile.admin.vo.ReceptorVo;
import cn.ynrcb.personnelfile.common.utils.PageUtils;
import cn.ynrcb.personnelfile.common.utils.R;
import cn.ynrcb.personnelfile.common.validator.ValidatorUtils;
import cn.ynrcb.personnelfile.common.validator.group.AddGroup;
import cn.ynrcb.personnelfile.sysadmin.controller.AbstractController;
import cn.ynrcb.personnelfile.sysadmin.dao.SysUserDao;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 通知表
 */
@Api(value = "整改需求系统",tags = {"业务相关=>通知相关接口"})
@RestController
@RequestMapping("admin/notice")
public class NoticeController extends AbstractController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private NoticeSendService noticeSendService;
    @Autowired
    private SysUserDao sysUserDao;

    /**
     * 批量已读
     */
    @ApiOperation("批量已读")
    @ApiImplicitParam(name = "ids", value = "通知关系ids",example ="1,2,3,4", required = true)
    @GetMapping("/markRead")
    public R markRead(String ids) {
        noticeSendService.markRead(ids);
        return R.ok();
    }

    /**
     * 全部已读
     */
    @ApiOperation("全部已读")
    @RequestMapping("/markAllReaded")
    public R markAllReaded(){
        noticeSendService.markAllReaded(getCurrentUserId());
        return R.ok();
    }

    /**
     * 未读数量
     */
    @GetMapping("/needReadNum")
    public R needReadNum() {
        return R.ok().put("needReadNum", noticeSendService.needReadNum(getCurrentUserId()));
    }

    /**
     * 列表
     */
    @ApiOperation("列表")
    @GetMapping("/list")
    @RequiresPermissions("admin:notice:list")
    public R list(@RequestParam Map<String, Object> params) {

        PageUtils page = noticeService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 我的消息列表
     */
    @ApiOperation("我的消息列表")
    @GetMapping("/myList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码",example ="1", required = false),
            @ApiImplicitParam(name = "limit", value = "条数",example ="12", required = false, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "通知类型",example ="1", required = false, dataType = "String"),
            @ApiImplicitParam(name = "isRead", value = "是否已读",example ="1", required = false, dataType = "String"),
            @ApiImplicitParam(name = "keyword", value = "关键字",example ="XXX", required = false, dataType = "String")
    })
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(value = "状态码",name = "code",example = "0"),
                    @DynamicParameter(value = "信息",name = "msg",example = "success"),
                    @DynamicParameter(value = "分页信息",name = "page",dataTypeClass = PageUtils.class),
                    @DynamicParameter(value = "我的消息列表",name = "page中的list",dataTypeClass = NoticeListVo.class)
            })
    )
    public R myList(@RequestParam Map<String, Object> params) {
        params.put("userId",getUserId());
        PageUtils page = noticeService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 首页消息列表
     */
    @ApiOperation("首页消息列表")
    @GetMapping("/indexList")
    public R indexList(@RequestParam Map<String, Object> params) {
        params.put("userId",getUserId());
        PageUtils page = noticeService.queryPageIndex(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取接收人列表
     */
    @ApiOperation("获取接收人列表")
    @ApiImplicitParam(name = "keyword", value = "关键字",example ="黄XX", required = false, dataType = "String")
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(value = "状态码",name = "code",example = "0"),
                    @DynamicParameter(value = "信息",name = "msg",example = "success"),
                    @DynamicParameter(value = "接收人列表",name = "receptorList",dataTypeClass = ReceptorVo.class)
            })
    )
    @GetMapping("/receptor")
    @RequiresPermissions("admin:notice:receptor")
    public R receptor() {
        List<ReceptorVo> receptorList = sysUserDao.queryReceptors();
        return R.ok().put("receptorList",receptorList);
    }

    /**
     * 通知内容
     */
    @ApiOperation("通知内容")
    @ApiImplicitParam(name = "id", value = "通知id",example ="152", required = true, dataType = "Integer")
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(value = "状态码",name = "code",example = "0"),
                    @DynamicParameter(value = "信息",name = "msg",example = "success"),
                    @DynamicParameter(value = "通知内容",name = "notice",dataTypeClass = NoticeVo.InfoResp.class)
            })
    )
    @GetMapping("/info/{id}")
    @RequiresPermissions("admin:notice:info")
    public R info(@PathVariable("id") Long id) {
        NoticeVo.InfoResp notice = noticeService.noticeInfo(id);

        //设置已读
        if (!notice.getIsRead() && notice.getUserId() == getUserId()) {
            noticeSendService.updateNoticeSendRead(id, getCurrentUserId());
        }

        return R.ok().put("notice", notice);
    }

    /**
     * 新增通知
     */
    @ApiOperation("新增通知")
    @PostMapping("/save")
    @RequiresPermissions("admin:notice:save")
    public R save(@RequestBody NoticeVo.saveParam params) {
        ValidatorUtils.validateEntity(params, AddGroup.class);

        noticeService.saveNotice(params);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/adminUpdate")
    @RequiresPermissions("admin:notice:adminUpdate")
    public R adminUpdate(@RequestBody NoticeEntity notice) {
//        ValidatorUtils.validateEntity(notice, UpdateGroup.class);
        noticeService.updateNoticeById(notice);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("admin:notice:delete")
    public R delete(@RequestBody Long[] ids) {
        noticeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/getCheckedTree/{id}")
    public R getCheckedTree(@PathVariable("id") Integer id) {
        List<NoticeSendEntity> list = noticeService.getCheckedTree(id);
        return R.ok().put("checkedTree", list);
    }

    /**
     * 所有通知列表
     * @param params page和limit
     * @return 通知列表
     */
    @ApiOperation("所有通知列表")
    @GetMapping("/adminList")
    @RequiresPermissions("admin:notice:adminList")
    public R adminList(@RequestParam Map<String, Object> params){
        PageUtils page = noticeService.queryAdminPage(params);
        return R.ok().put("page", page);
    }
    /**
     * 删除
     */
    @RequestMapping("/adminDelete")
    @RequiresPermissions("admin:notice:adminDelete")
    public R adminDelete(@RequestBody Integer[] ids) {
        noticeService.removeByIds(Arrays.asList(ids));
        noticeSendService.deleteNoticeSend(ids);
        return R.ok();
    }

}
